package cn.sfa.service.affix;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.ConfigUtil;
import cn.sfa.common.utils.MajorKeyGenerate;
import cn.sfa.common.utils.TableNameEnum;
import cn.sfa.entity.affixItem.SaleAffixItem;
import cn.sfa.entity.users.SaleUsersLogin;
  
/**
 * 
* 类名称：affixService   
* 类描述： 附件相关服务类
* 创建人：LY   
* 创建时间：2012-2-15 下午11:12:18    
* 修改备注：   
* @version
 */
@Service
@Transactional
public class AffixService {
	 
	static Logger logger = LoggerFactory.getLogger(AffixService.class);
	
	@Autowired
	NamedParameterJdbcTemplate jdbcTemplate;
	
	@Autowired
	MajorKeyGenerate majorKeyGenerate;
	  
	/*
	 *获取附件列表
	 */
	public List<SaleAffixItem> getAffix(String tabName,String mainId){
		String sqlStr = "select saleaffixitemid,CREATEDATE,CREATEUSER," +
				"UPDATEDATE,UPDATEUSER,AFFIXMAINTABNAME,AFFIXMAINID," +
				"SALEAFFIXITEMFILEPATH,SALEAFFIXITEMFILENAME,ISTRUE from saleaffixitem " +
				" where AFFIXMAINTABNAME = :AFFIXMAINTABNAME and AFFIXMAINID =:AFFIXMAINID and isTrue = 'Y'";

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("AFFIXMAINTABNAME", tabName);
		paramMap.put("AFFIXMAINID", mainId); 
		
 		List<SaleAffixItem> affixList=new ArrayList<SaleAffixItem>();
 		affixList=jdbcTemplate.query(sqlStr,paramMap,new BeanPropertyRowMapper<SaleAffixItem>(SaleAffixItem.class));
 		System.out.println(affixList.size());
		return affixList;
	}
	/*
	 * 插入附件
	 */
	public boolean insert(SaleAffixItem model){
		String sqlStr = " INSERT INTO saleaffixitem( SALEAFFIXITEMID,CREATEDATE,CREATEUSER,AFFIXMAINTABNAME,AFFIXMAINID,SALEAFFIXITEMFILEPATH,SALEAFFIXITEMFILENAME,ISTRUE) " +
		"VALUES(" +
		"default," +
		":CREATEDATE," +
		":CREATEUSER," +
		":AFFIXMAINTABNAME," +
		":AFFIXMAINID," +
		":SALEAFFIXITEMFILEPATH," +
		":SALEAFFIXITEMFILENAME," +
		":ISTRUE" +
		") ";
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("CREATEDATE", model.getCreateDate()); 
		paramMap.put("CREATEUSER", CommonUtil.jugeAndFixNull(model.getCreateUser())); 
		paramMap.put("AFFIXMAINTABNAME", CommonUtil.jugeAndFixNull(model.getAFFIXMAINTABNAME()));
		paramMap.put("AFFIXMAINID", CommonUtil.jugeAndFixNull(model.getAFFIXMAINID()));
		paramMap.put("SALEAFFIXITEMFILEPATH", CommonUtil.jugeAndFixNull(model.getSALEAFFIXITEMFILEPATH()));
		paramMap.put("SALEAFFIXITEMFILENAME", CommonUtil.jugeAndFixNull(model.getSALEAFFIXITEMFILENAME())); 
		paramMap.put("ISTRUE", CommonUtil.jugeAndFixNull(model.getIsTrue()));
		return jdbcTemplate.update(sqlStr,paramMap)>0;
	}
	
	/**
	 * 
	 * @Title: update 
	 * @author: LiXiang
	 * @Description: 修改附件
	 * @param model
	 * @return
	 * @throws
	 */
	public boolean update(SaleAffixItem model){
		String sqlStr = "update saleaffixitem set " +
				"affixmaintabname = :AFFIXMAINTABNAME, " +
				"affixmainid = :AFFIXMAINID, " +
				"istrue = :ISTRUE " +
				"where saleaffixitemid = :SALEAFFIXITEMID ";
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("AFFIXMAINTABNAME", CommonUtil.jugeAndFixNull(model.getAFFIXMAINTABNAME()));
		paramMap.put("AFFIXMAINID", CommonUtil.jugeAndFixNull(model.getAFFIXMAINID()));
		paramMap.put("ISTRUE", CommonUtil.jugeAndFixNull(model.getIsTrue()));
		paramMap.put("SALEAFFIXITEMID", model.getSALEAFFIXITEMID()); 
		  
		return jdbcTemplate.update(sqlStr,paramMap)>0;
	}
	
	/*
	 * 根据附件ID获取附件路径
	 */
	public String getAffixPathById(String mainId){
		
		String sqlStr = "select saleaffixitemfilepath from saleaffixitem where saleaffixitemid =:AFFIXMAINID ";
		
		String affixpath = null;
		 Map<String, Object> paramMap = new HashMap<String, Object>();
		 paramMap.put("AFFIXMAINID", mainId);
		 affixpath = jdbcTemplate.queryForObject(sqlStr, paramMap,new RowMapper<String>(){
			public String mapRow(ResultSet rs, int rowNum) throws SQLException {				
				return rs.getString("SALEAFFIXITEMFILEPATH");
			}			 
		 });
		 return affixpath;
	}
	

	/*
	 * 根据附件ID获取附件所有信息
	 */
	public SaleAffixItem getAffixItemById(String SALEAFFIXITEMID){
		String sqlStr = "select SALEAFFIXITEMID,CREATEDATE,CREATEUSER," +
		"UPDATEDATE,UPDATEUSER,AFFIXMAINTABNAME,AFFIXMAINID," +
		"SALEAFFIXITEMFILEPATH,SALEAFFIXITEMFILENAME from saleaffixitem " +
		" where SALEAFFIXITEMID =:SALEAFFIXITEMID ";

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("SALEAFFIXITEMID", SALEAFFIXITEMID); 
		
		final SaleAffixItem affixInfo = new SaleAffixItem();
		jdbcTemplate.query(sqlStr.toString(), paramMap,
				new RowCallbackHandler() {

					public void processRow(ResultSet rs) throws SQLException {
						affixInfo.setSALEAFFIXITEMID(rs.getString("SALEAFFIXITEMID"));//记录ID
						affixInfo.setCreateDate(rs.getDate("CREATEDATE"));//创建日期
						affixInfo.setCreateUser(rs.getString("CREATEUSER"));//创建用户
						affixInfo.setUpdateDate(rs.getDate("UPDATEDATE"));//更新日期
						affixInfo.setUpdateUser(rs.getString("UPDATEUSER"));//创建用户
						affixInfo.setAFFIXMAINTABNAME(rs.getString("AFFIXMAINTABNAME"));//主表表名
						affixInfo.setAFFIXMAINID(rs.getString("AFFIXMAINID"));//主表记录ID
						affixInfo.setSALEAFFIXITEMFILEPATH(rs.getString("SALEAFFIXITEMFILEPATH"));//附件路径
						affixInfo.setSALEAFFIXITEMFILENAME(rs.getString("SALEAFFIXITEMFILENAME"));//附件文件名
					}
				});
		return affixInfo;
	} 
	
	
	/*
	 * 根据附件Name获取附件所有信息
	 */
	public SaleAffixItem getAffixItemByName(String Name){
		String sqlStr = "select SALEAFFIXITEMID,CREATEDATE,CREATEUSER," +
		"UPDATEDATE,UPDATEUSER,AFFIXMAINTABNAME,AFFIXMAINID," +
		"SALEAFFIXITEMFILEPATH,SALEAFFIXITEMFILENAME from saleaffixitem " +
		" where SALEAFFIXITEMFILENAME =:Name ";

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("Name", Name); 
		
		final SaleAffixItem affixInfo = new SaleAffixItem();
		jdbcTemplate.query(sqlStr.toString(), paramMap,
				new RowCallbackHandler() {

					public void processRow(ResultSet rs) throws SQLException {
						affixInfo.setSALEAFFIXITEMID(rs.getString("SALEAFFIXITEMID"));//记录ID
						affixInfo.setCreateDate(rs.getDate("CREATEDATE"));//创建日期
						affixInfo.setCreateUser(rs.getString("CREATEUSER"));//创建用户
						affixInfo.setUpdateDate(rs.getDate("UPDATEDATE"));//更新日期
						affixInfo.setUpdateUser(rs.getString("UPDATEUSER"));//创建用户
						affixInfo.setAFFIXMAINTABNAME(rs.getString("AFFIXMAINTABNAME"));//主表表名
						affixInfo.setAFFIXMAINID(rs.getString("AFFIXMAINID"));//主表记录ID
						affixInfo.setSALEAFFIXITEMFILEPATH(rs.getString("SALEAFFIXITEMFILEPATH"));//附件路径
						affixInfo.setSALEAFFIXITEMFILENAME(rs.getString("SALEAFFIXITEMFILENAME"));//附件文件名
					}
				});
		return affixInfo;
	} 
	
	
	/**
	 * 
	 * @Title: copyFileAndgetAffixModel 
	 * @author: LiXiang
	 * @Description: 将临时文件夹下的文件保存到上传目录
	 * @param path 公共目录
	 * @param oldPath 源文件
	 * @param newPath 目标文件
	 * @param fileName 文件名 （存入数据库中的文件名也是显示在页面上的文件名）
	 * @param majorKeyGenerate
	 * @param userId 用户ID号
	 * @param affixMainId 附件ID号
	 * @return 返回附件实例
	 * @throws Exception
	 */
	public SaleAffixItem copyFileAndgetAffixModel(String path, String oldPath, String newPath,String fileName, MajorKeyGenerate majorKeyGenerate, String userId, String affixMainId) throws Exception{
		SaleAffixItem affixModel = new SaleAffixItem();
		int bytesum = 0;
		int byteread = 0;
		String fullOldPath = path+oldPath;
		String fullNewPath = path+newPath;
		File oldfile = new File(fullOldPath);
		String[] fileNames = (fullOldPath).split("/");
		String fileNameTemp = fileNames[fileNames.length - 1];
		fullNewPath = fullNewPath.replaceAll(fileNameTemp, "");
		File newfile = new File(fullNewPath);
		
		try {
			if (oldfile.exists()) { // 文件存在时
				if (!newfile.exists())
					newfile.mkdirs();
				InputStream inStream = new FileInputStream(fullOldPath); // 读入原文件 
				FileOutputStream fs = new FileOutputStream(fullNewPath + "//" + fileNameTemp);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread; // 字节数 文件大小
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new Exception();
		}
		
		affixModel.setSALEAFFIXITEMID(majorKeyGenerate.keyGenerate(TableNameEnum.SALEAFFIXITEM));// 主键ID
		affixModel.setCreateDate(new Date());// 创建日期
		affixModel.setCreateUser(userId);// 创建用户
		affixModel.setAFFIXMAINTABNAME("SALEPRODUCT");
		affixModel.setAFFIXMAINID(affixMainId);
		affixModel.setSALEAFFIXITEMFILEPATH(newPath);// 路径
		affixModel.setSALEAFFIXITEMFILENAME(fileName);// 文件名
		return affixModel;
	}
	
	/**
	 * 
	 * @Title: uploadAffix 
	 * @author: LiXiang
	 * @Description: TODO
	 * @param request
	 * @param response
	 * @param maxPostSize 文件最大KB数 如：int maxPostSize = 10 * 1024 * 1024 = 10M;
	 * @param affixType 上传类型
	 * @return
	 * @throws
	 */
	public String uploadAffix(HttpServletRequest request,HttpServletResponse response,int maxPostSize,String affixType){
		String message = "";
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
		servletFileUpload.setHeaderEncoding(request.getCharacterEncoding());
		servletFileUpload.setSizeMax(maxPostSize);
		// 文件下载的目录
		String path ="";
		try {
			String xmlPath = AffixService.class.getClassLoader().getResource("/sys_config.xml").getPath();
			Configuration config = ConfigUtil.getXmlConfig(xmlPath);
			path = config.getString("upload-url");
		} catch (Exception e) {
			throw new RuntimeException("读取配置文件有误！");
		}

		// 文件上传后需置于的目录
		String detailDir = "uploadfiles/"+affixType+"/";
		String dir = path + detailDir;

		// 根据年,月,日划分文件夹
		GregorianCalendar cal = new GregorianCalendar();

		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DATE);

		List<?> fileItems;
		try {
			fileItems = servletFileUpload.parseRequest(request);
		} catch (Exception ex) {
			try {
				message = "{result:false,message:'"+ URLEncoder.encode("上传的文件不能超过10M","utf-8") +"'}";
			} catch (UnsupportedEncodingException e) {
				throw new RuntimeException();
			}
			return message;
		}
		SaleAffixItem affixModel = new SaleAffixItem();
		try {

			Iterator<?> iter = fileItems.iterator();
			while (iter.hasNext()) {

				FileItem item = (FileItem) iter.next();

				if (!item.isFormField()) {
					String uploadfileName = item.getName();
					
					//判断request传过来的路径是什么格式，如果是IE　item.getName()并不是文件名，需要转换格式
					String[] uploadFileNames = uploadfileName.split("\\\\");
					if(uploadFileNames.length>0){
						uploadfileName = uploadFileNames[uploadFileNames.length-1];
					}
					int lastIndexOf = uploadfileName.lastIndexOf(".");
					int lastIndexOf2 = uploadfileName.lastIndexOf("/") + 1;
					lastIndexOf2 = lastIndexOf2 == 0 ? 0 : lastIndexOf2;
					String extName = uploadfileName.substring(lastIndexOf);// 获取扩展名
					String pname = uploadfileName.substring(lastIndexOf2, lastIndexOf);// 上传的文件名

					// 组合路径
					String uploadDir = dir + year + "/" + month + "/" + day;
					File file = new File(uploadDir);
					if (!file.exists()) {// 如果文件不存在,则创建目录
						file.mkdirs();
					}
					String currentTimeMillis = String.valueOf(System.currentTimeMillis());
					String fileName = year + "/" + month + "/" + day + "/" + currentTimeMillis + "_" + pname + extName + ".hktemp";
					String filePath = dir + fileName;
					InputStream is = item.getInputStream();// 读取流文件
					BufferedOutputStream write = new BufferedOutputStream(new FileOutputStream(filePath));

					// 流文件的对拷
					byte[] buf = new byte[1024];// 定义一个1k的缓冲数组
					int len = -1;
					while ((len = is.read(buf)) != -1) {// -1为文件结束标志
						write.write(buf, 0, len);
					}
					write.close();
					is.close();

					SaleUsersLogin user = (SaleUsersLogin) request.getSession().getAttribute("user");
					affixModel.setSALEAFFIXITEMFILENAME(pname + extName); // 文件名
					affixModel.setSALEAFFIXITEMFILEPATH(detailDir + fileName); // 文件路径
					affixModel.setIsTrue("N"); // N表示只上传但用户未确认保存
					affixModel.setCreateDate(new Date());// 创建日期
					affixModel.setAFFIXMAINTABNAME(affixType); // 上传目录或类型
					affixModel.setCreateUser(user.getSaleUsersLoginId());// 创建用户
					this.insert(affixModel);
					
					affixModel = this.getAffixItemById(affixModel.getSALEAFFIXITEMFILENAME());

					message = "{result:true,message:'" + URLEncoder.encode(pname,"utf-8") + extName + "',saleaffixitemid:'" + affixModel.getSALEAFFIXITEMID() + "'}";
				}
			}

		} catch (Exception ex) {
			message = "{result:false}";
		}
		return message;
	}
	
	/**
	 * 上传图片
	 * TODO
	 * @author: LiXiang
	 * @param request
	 * @param response
	 * @param maxPostSize
	 * @param affixType
	 * @param path
	 * @param agentId
	 * @return
	 */
	public String uploadPicture(HttpServletRequest request,HttpServletResponse response,int maxPostSize,String affixType,String path,String agentId){
		String message = "";
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
		servletFileUpload.setHeaderEncoding(request.getCharacterEncoding());
		servletFileUpload.setSizeMax(maxPostSize);

		// 文件上传后需置于的目录
		String detailDir = "/UserHeadImage/"+affixType+"/";
		String dir = path + detailDir;

		List<?> fileItems;
		try {
			fileItems = servletFileUpload.parseRequest(request);
		} catch (Exception ex) {
			message = null;
			return message;
		}
		SaleAffixItem affixModel = new SaleAffixItem();
		try {

			Iterator<?> iter = fileItems.iterator();
			while (iter.hasNext()) {

				FileItem item = (FileItem) iter.next();

				if (!item.isFormField()) {
					String uploadfileName = item.getName();
					
					//判断request传过来的路径是什么格式，如果是IE　item.getName()并不是文件名，需要转换格式
					String[] uploadFileNames = uploadfileName.split("\\\\");
					if(uploadFileNames.length>0){
						uploadfileName = uploadFileNames[uploadFileNames.length-1];
					}
					int lastIndexOf = uploadfileName.lastIndexOf(".");
					int lastIndexOf2 = uploadfileName.lastIndexOf("/") + 1;
					lastIndexOf2 = lastIndexOf2 == 0 ? 0 : lastIndexOf2;
					String extName = uploadfileName.substring(lastIndexOf);// 获取扩展名
					String pname = uploadfileName.substring(lastIndexOf2, lastIndexOf);// 上传的文件名

					// 组合路径
					String uploadDir = dir + agentId;
					File file = new File(uploadDir);
					if (!file.exists()) {// 如果文件不存在,则创建目录
						file.mkdirs();
					}
					String currentTimeMillis = String.valueOf(System.currentTimeMillis());
					String fileName = agentId + "/" + currentTimeMillis + "_" + pname + extName;
					String filePath = dir + fileName;
					InputStream is = item.getInputStream();// 读取流文件
					BufferedOutputStream write = new BufferedOutputStream(new FileOutputStream(filePath));

					// 流文件的对拷
					byte[] buf = new byte[1024];// 定义一个1k的缓冲数组
					int len = -1;
					while ((len = is.read(buf)) != -1) {// -1为文件结束标志
						write.write(buf, 0, len);
					}
					write.close();
					is.close();

					SaleUsersLogin user = (SaleUsersLogin) request.getSession().getAttribute("user");
					String generateId = majorKeyGenerate.keyGenerate(TableNameEnum.SALEAFFIXITEM);
					affixModel.setSALEAFFIXITEMFILENAME(pname + extName); // 文件名
					affixModel.setSALEAFFIXITEMFILEPATH(detailDir + fileName); // 文件路径
					affixModel.setSALEAFFIXITEMID(generateId); // 生成ID号
					affixModel.setIsTrue("N"); // N表示只上传但用户未确认保存
					affixModel.setCreateDate(new Date());// 创建日期
					affixModel.setAFFIXMAINTABNAME(affixType); // 上传目录或类型
					affixModel.setCreateUser(user.getSaleUsersLoginId());// 创建用户
					affixModel.setAFFIXMAINID(agentId);// 创建用户
					this.insert(affixModel);

					message = detailDir + fileName;
				}
			}

		} catch (Exception ex) {
			message = null;
		}
		return message;
	}
	
	/**
	 * 
	 * @Title: saveAffix 
	 * @author: LiXiang
	 * @Description: 保存附件信息（将用户仅页面上传且点保存后的附件的状态isTrue设为真）
	 * @param affixStr  构建字符串如： affixStr = SALEAFFIXITEMID1;isDel1;;SALEAFFIXITEMID2;isDel2;;
	 * @param affixType 保存的附件类型
	 * @param affixTypeId  保存的附件类型的ID(注：并非附件ID)
	 * @throws Exception
	 * @throws
	 */
	public void saveAffix(String affixStr,String affixType,String affixTypeId) throws Exception{
		List<SaleAffixItem> affixList = new ArrayList<SaleAffixItem>();
		if (!CommonUtil.isEmpty(affixStr.trim())) {
			String[] affixArray = affixStr.trim().split(";;");
			for (String aStr : affixArray) {
				if (!CommonUtil.isEmpty(aStr.trim())) {
					String[] affixInfo = aStr.split(";");
					if (affixInfo.length == 2
							&& !CommonUtil.isEmpty(affixInfo[0])
							&& !CommonUtil.isEmpty(affixInfo[1])) {
						SaleAffixItem affixModel = new SaleAffixItem();
						affixModel.setSALEAFFIXITEMID(affixInfo[0].trim());// 主键ID
						affixModel.setAFFIXMAINTABNAME(affixType);
						affixModel.setAFFIXMAINID(affixTypeId);
						if("n".equals(affixInfo[1].trim()))
							affixModel.setIsTrue("Y");// 是否真保存
						else affixModel.setIsTrue("N");// 是否真保存
						affixList.add(affixModel);
					}
				}
			}
		}
		for (SaleAffixItem affixItem : affixList) {
			this.update(affixItem);
		}
	}
	
	
	/**
	 * 
	 * @Title: saveAffixPicture 
	 * @author: LiXiang
	 * @Description: 保存图片信息（将用户仅页面上传且点保存后的附件的状态isTrue设为真）
	 * @param affixStr  构建字符串如： affixStr = SALEAFFIXITEMID1;isDel1;;SALEAFFIXITEMID2;isDel2;;
	 * @param affixType 保存的附件类型
	 * @param affixTypeId  保存的附件类型的ID(注：并非附件ID)
	 * @throws Exception
	 * @throws
	 */
	public void saveAffixPicture(HttpServletRequest request,HttpServletResponse response,String affixType,String affixTypeId,String filePath) throws Exception {
		
		//step1 : 将所有以前的头像设为 istrue=n
		String sqlStr = "update saleaffixitem set " +
		"istrue = :ISTRUE " +
		"where affixmainid = :AFFIXMAINID " +
		"and affixmaintabname = :AFFIXMAINTABNAME ";
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("AFFIXMAINTABNAME", affixType);
		paramMap.put("AFFIXMAINID", affixTypeId);
		paramMap.put("ISTRUE", "N");
		jdbcTemplate.update(sqlStr, paramMap);
		
		//step2 : 将当前的头像设为 istrue=y
		SaleUsersLogin user = (SaleUsersLogin) request.getSession().getAttribute("user");
		String generateId = majorKeyGenerate.keyGenerate(TableNameEnum.SALEAFFIXITEM);
		SaleAffixItem affixModel = new SaleAffixItem();
		affixModel.setSALEAFFIXITEMFILEPATH(filePath); // 文件路径
		affixModel.setSALEAFFIXITEMID(generateId); // 生成ID号
		affixModel.setIsTrue("Y"); // N表示只上传但用户未确认保存
		affixModel.setCreateDate(new Date());// 创建日期
		affixModel.setAFFIXMAINTABNAME(affixType); // 上传目录或类型
		affixModel.setAFFIXMAINID(affixTypeId); // 上传目录或类型
		affixModel.setCreateUser(user.getSaleUsersLoginId());// 创建用户
		affixModel.setCreateUser(user.getSaleUsersLoginId());// 创建用户
		this.insert(affixModel);
		
	}
}
